var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isshowCalendar: false,
    isshowporss: false,
    current: 0,
    isonLoad:true,
    // 我的家项目列表
    myHomeList: [],
    sign: [{
        data: '2022-8-3',
        color: '#45B1ED',
        text: '设计'
      },
      {
        data: '2022-8-09',
        color: '#FFA940',
        text: '配送'
      },
      {
        data: '2022-8-14',
        color: '#7CB305',
        text: '施工'
      },
    ],
    yjsign: [{
      data: '2022-8-30',
      color: '#7CB305',
      text: '计划完成时间'
    }],
    startdate: {
      date: '2022-8-7',
      color: '#D6000F',
      text: '完工'
    },
    starttime: '2022-07-03',
    endtime: '2023-01-25',
    iscustomerGrade:false
  },
  bindtabright(){
   
    let current=this.data.current+1
    if(current>=this.data.myHomeList.length){
      current=0
    }
    this.setData({
      current:current
    })
    this.getOrderDetails(this.data.myHomeList[current].id)
  },
  bindtaleft(){
    let current=this.data.current-1
    if(current<0){
      current=this.data.myHomeList.length-1
    }
    this.setData({
      current:current
    })
    this.getOrderDetails(this.data.myHomeList[current].id)
  },
  // 图片预览
  priview(e){
    let urls=e.currentTarget.dataset.urls.map(item=>this.data.Imageurl+item)
    let current=e.currentTarget.dataset.current
    wx.previewImage({
      urls: urls,
      current:current
    })
  },


  // 时间处理

  mytime(statedate, enddate, whendate) {
    var datse = new Date();
    var year = datse.getFullYear(); //  返回的是年份
    var month = datse.getMonth() + 1; //  返回的月份上个月的月份，记得+1才是当月
    var day = datse.getDate(); //  返回的是几号

    if (statedate != '' && enddate != '') {
      // 开始时间戳
      var statedates = new Date(statedate);
      // 计划结束时间戳
      var enddates = new Date(enddate);


      // 计划时间
      var planTime = Math.ceil((enddates - statedates) / (24 * 1000 * 60 * 60)) + 1

      // 剩余时间
      var remaining = Math.ceil((enddates - datse) / (24 * 1000 * 60 * 60)) + 1
      var overdueTime
      if (remaining < 1) {
        overdueTime = Math.abs(remaining - 1)
      } else {
        overdueTime = 0
      }
    }



    if (whendate != '') {
      // 结束时间戳
      var whendates = new Date(whendate);
      // 所用时间
      var when = Math.ceil((whendates - statedates) / (24 * 1000 * 60 * 60)) + 1

    }
    // month+'月'+day+'日'

    return {
      month: month,
      day: day,
      planTime: statedate != '' && enddate != '' ? planTime : '',

      remaining: statedate != '' && enddate != '' ? remaining : '',
      when: whendate != '' ? when : '',
      overdueTime: overdueTime,
      overdueTime: overdueTime
    }


  },
  // 项目详情接口
  getOrderDetails(orderId) {
    let t = this
    wx.showLoading({
      title: '加载中。。。', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'getOrderDetails',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        // userId: 1569562832576524288,
        orderId: orderId,
      },
      success: res => {
        wx.hideLoading();
        console.log(res)
        t.setData({
          projectOrder: res.data.projectOrder,
          designList: res.data.designList,
          deliverList: res.data.deliverList,
          buildList: res.data.buildList,
          workList: res.data.workList.filter(item=>item.bricklayId!=''),
          shopowner: res.data.shopowner,
          supervise: res.data.supervise,
          bricklay: res.data.bricklay,
          designer: res.data.designer,
          adviser: res.data.adviser,
          // bricklayList:res.data.bricklayList,
          mytime: t.mytime(res.data.projectOrder.bricklayBeginDate, res.data.projectOrder.bricklayEndDate, res.data.projectOrder.checkDate),
          starttime: res.data.projectOrder.bricklayBeginDate,
          endtime: res.data.projectOrder.bricklayEndDate,

          sign: [{
              data: res.data.projectOrder.designDate,
              color: '#FFA940',
              text: '设计'
            },
            {
              data: res.data.projectOrder.deliverDate,
              color: '#7CB305',
              text: '配送'
            },
            {
              data: res.data.projectOrder.buildDate,
              color: '#45B1ED',
              text: '施工'
            },
          ],
          // startTime:"2022-11-03",
          // endTime:"2022-12-02",
          startdate: {
            date: res.data.projectOrder.checkDate,
            color: '#D6000F',
            text: '完工'
          },
          yjsign: [{
            data: '',
            color: '#D6000F',
            text: '计划完工日期'
          }],
        })




    


      
  // 判断用户已经评价了
  if (res.data.projectOrder.customerGrade != '') {
    t.setData({
      iscustomerGrade: true,
   
    })
  }else{
    t.setData({
      iscustomerGrade: false,
  
    })
  }


      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
      }
    })
  },


 
  // 我的家
  getMyHome() {
    let t = this
    console.log(app.globalData.userId)
    wx.showLoading({
      title: '加载中。。。', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'getMyHome',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
      },
      success: res => {
        wx.hideLoading();
        console.log(res)
        if (res.data.result == 0) {
          this.setData({
            myHomeList: res.data.myHomeList
          })
          this.getOrderDetails(this.data.myHomeList[0].id)
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
        }





      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },


  // 跳转到评价
  bindgoevaluation(e) {
    wx.navigateTo({
      url: '/customer/pages/userEvaluation/userEvaluation?orderId='+this.data.myHomeList[this.data.current].id,
    })
  },
  // 显示完整日历
  bindeShowCalender() {
    this.setData({
      isshowCalendar: true
    })
    let calendar = this.selectComponent('#calendar');
    calendar.Rendering();
  },
  // 隐藏完整日历
  bindeHideCalender() {
    this.setData({
      isshowCalendar: false
    })
  },
  // 施工日志进度条
  bindeShowporss() {
    this.setData({
      isshowporss: true
    })
  },
  // 隐藏施工日志进度条
  bindeHideporss() {
    this.setData({
      isshowporss: false
    })
  },
  gopersonal(e){
    let id=e.currentTarget.dataset.id
    let people=e.currentTarget.dataset.item
    console.log(people)
      wx.navigateTo({
        url: '/pages/personal/personal?type='+e.currentTarget.dataset.type+'&&id='+id+"&&people="+JSON.stringify(people),
      })

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    let myTab = this.selectComponent('#myTab'); // 页面获取自定义组件实例
    myTab.gettabList(); // 通过实例调用组件事件
    this.getMyHome()
    this.setData({
      Imageurl:app.globalData.Imageurl,
      identity:app.globalData.identity
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let myTab = this.selectComponent('#myTab'); // 页面获取自定义组件实例
    myTab.gettabList(); // 通过实例调用组件事件
    this.getMyHome()
    this.setData({
      identity:app.globalData.identity
    })
    if(!this.data.isonLoad){
      if(wx.getStorageSync('istab')){
        let myTab = this.selectComponent('#myTab'); // 页面获取自定义组件实例
   
        this.setData({
          isshowCalendar:false
        })
      }
    }else{
      this.setData({
        isonLoad:false
      })
    }
    wx.setStorageSync('istab', false)
  },  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})